package main

import (
	"fmt"
	"runtime"
	"strconv"
	"time"
)

func main() {
	fmt.Println(runtime.NumCPU())
	n := runtime.GOMAXPROCS(2) //限定使用CPU的核数
	fmt.Println(n)
	n = runtime.GOMAXPROCS(4) //限定使用CPU的核数
	fmt.Println(n)
	n = runtime.GOMAXPROCS(6) //限定使用CPU的核数
	fmt.Println(n)
	fmt.Println(runtime.NumCPU())
	//return
	for i := 0; i < 10; i++ {
		go sayHello(i)
	}
	time.Sleep(4 * time.Second)
}

func sayHello(i int) {
	for k := 0; k < 10; k++ {
		if i == 2 {
			runtime.Gosched()
		}
		if i == 2 && k == 5 {
			runtime.Goexit()
		}
		fmt.Println("协程" + strconv.Itoa(i) + ":" + strconv.Itoa(k))
	}
}
